<?php


namespace app\engine\controller;


use cmf\controller\HomeBaseController;
use Exception;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;

class GroupController extends HomeBaseController
{
    public function index()
    {
        $id = $this->request->param();
        $data = Db::name('group')->order('type desc')->select()->toArray();
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function add()
    {
        // $role = Db::name('role_user')->where(['user_id'=>cmf_get_current_admin_id()])->value('role_id');
        // if($role == 2 || $role == 5){
        //     $this->error('无权限操作');
        // }
        return $this->fetch();
    }

    /**
     * 执行新增
     */
    public function doadd()
    {
        $data = $this->request->param();
        $files = request()->file('image');
        unset($data['image']);
        $kps = [];

        if (empty($data['name']) || empty($data['type']) || empty($data['mobile']) || empty($data['card']) || empty($data['address'])) {
            $this->error('请填写参数');
        }
        $check = Db::name('group')->where(['name' => $data['name']])->find();
        if ($check) {
            $this->error('姓名已存在');
        }
        $checkmobile = Db::name('group')->where(['mobile' => $data['mobile']])->find();
        if ($checkmobile) {
            $this->error('手机号已存在');
        }
        if ($files) {
            foreach ($files as $k => $file) {
                // 移动到框架应用根目录/public/uploads/ 目录下
                $info = $file->move(CMF_ROOT . 'public' . '/' . 'uploads');
                if ($info) {
                    $kps[$k] = '/uploads/' . date('Ymd', time()) . '/' . $info->getFilename();
                } else {
                    $this->error($file->getError());
                }
            }
            $data['imgs'] = implode(',', $kps);
        }
        $res = Db::name('group')->insert($data);
        if (!$res) {
            $this->error('添加失败');
            $img = explode(',', $data['imgs']);
            foreach ($img as $k => $v) {
                $v = CMF_ROOT . $v;
                if (file_exists($v)) {
                    unlink($v);
                }
            }
        }
        $this->success('添加成功');
    }

    /**
     * 单条记录查看
     * @return mixed|string
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function see()
    {
        $data = $this->request->param();
        $res = Db::name('group')->where('id', $data['id'])->find();
        $res['imgs'] = explode(',', $res['imgs']);
        foreach ($res['imgs'] as $k => &$v) {
            $v = 'http://' . $_SERVER['SERVER_NAME'] . $v;
        }
        $res['insTime'] = date('Y-m-d', strtotime($res['insTime']));
        $this->assign('res', $res);
        return $this->fetch();
    }

    public function del()
    {
        // $role = Db::name('role_user')->where(['user_id'=>cmf_get_current_admin_id()])->value('role_id');
        // if($role == 2 || $role == 5 || $role == 3){
        //     $this->error('无权限操作');
        // }
        $data = $this->request->param();
        $check = Db::name('group')->where('id', $data['id'])->value('imgs');
        $cp = explode(',', $check);
        foreach ($cp as $v) {
            $path = CMF_ROOT . $v;
            if ($path != CMF_ROOT && file_exists($path)) {
                unlink($path);
            }
        }
        Db::startTrans();
        try {
            Db::name('group')->where('id', $data['id'])->delete();
        } catch (Exception $e) {
            Db::rollback();
            $this->error('操作失败');
        }
        Db::commit();
        $this->success('操作成功');
    }


    public function edit()
    {
        // $role = Db::name('role_user')->where(['user_id'=>cmf_get_current_admin_id()])->value('role_id');
        // if($role == 2 || $role == 5){
        //     $this->error('无权限操作');
        // }
        $data = $this->request->param();

        $id = $data['id'];
        unset($data['id']);
        $files = request()->file('image');
        if ($files) {
            $check = Db::name('group')->where('id', $id)->value('imgs');
            $cp = explode(',', $check);
            foreach ($cp as $v) {
                if ($v) {
                    $path = CMF_ROOT  . '/' . $v;
                    if ($path != CMF_ROOT && file_exists($path)) {
                        unlink($path);
                    }
                }
            }
            $kps = [];
            foreach ($files as $k => $file) {
                // 移动到框架应用根目录/public/uploads/ 目录下
                $info = $file->move(CMF_ROOT . 'public' . '/' . 'uploads');
                if ($info) {
                    $kps[$k] = '/uploads/' . date('Ymd', time()) . '/' . $info->getFilename();
                } else {
                    $this->error($file->getError());
                }
            }
            $data['imgs'] = implode(',', $kps);
        } else {
            unset($data['image']);
        }

        if (empty($data['name']) || empty($data['type']) || empty($data['mobile']) || empty($data['address'])) {
            $this->error('请填写参数');
        }

        $res = Db::name('group')->where('id', $id)->update($data);
        if (!$res) {
            $this->error('操作失败');
        }
        $this->success('操作成功');
    }
}
